package com.toulezu.test.schedule;

import com.toulezu.test.task.MyTask;
import com.toulezu.test.trigger.CustomTrigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Configuration
//@EnableScheduling // 标注启动定时任务
public class SpringScheduleWithSchedulingConfigurer implements SchedulingConfigurer {

	private final String cron = "*/1 * * * * ?";

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		// 自定义定时任务的执行线程池
		taskRegistrar.setScheduler(taskScheduler());
		// 具体的任务
		taskRegistrar.addTriggerTask(() -> myTask().work(), new CustomTrigger(cron));
	}

	@Bean(destroyMethod = "shutdown")
	public Executor taskScheduler() {
		return Executors.newScheduledThreadPool(5);
	}

	@Bean
	public MyTask myTask() {
		return new MyTask();
	}
}


